package io.github.lee.jel.utils.ext

import android.graphics.drawable.Drawable
import android.widget.ImageView
import androidx.core.content.res.ResourcesCompat
import androidx.databinding.BindingAdapter
import coil.transform.RoundedCornersTransformation
import io.github.lee.core.ui.ba.coilLoad
import io.github.lee.core.util.toPx
import io.github.lee.jel.R
import io.github.lee.repository.jel.domain.People


@BindingAdapter(
    value = ["loadPeopleAvatar", "crossfade_time", "placeholder_res", "error_res"],
    requireAll = false
)
fun ImageView.loadPeopleAvatar(
    people: People?,
    crossfade: Int? = null,
    placeholder: Drawable? = null,
    error: Drawable? = null
) {
    val drawable = ResourcesCompat.getDrawable(context.resources, R.mipmap.ic_default_avatar, null)
    val t = RoundedCornersTransformation(4.toPx(context) * 1.0F)
    coilLoad(
        people?.fullAvatar(context),
        crossfade ?: 180,
        placeholder ?: drawable,
        error ?: drawable,
        t
    )
}